package com.punai.dataanalysis.epdatakb.service.NG001;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.punai.dataanalysis.epdatakb.bo.NG001.SjxyfxBasic;
import com.punai.dataanalysis.epdatakb.vo.DataAnalysisVo;
import com.punai.dataanalysis.ng001.mapper.SjxyfxMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class NG001SjxyfxService {

    @Autowired
    private SjxyfxMapper sjxyfxMapper;

    /**
     *
     * @return
     */
    public List<SjxyfxBasic> getBasicAndCzCbInfo(DataAnalysisVo dataAnalysisVo) {
        List<SjxyfxBasic> basicAndCz = sjxyfxMapper.getBasicAndCz(dataAnalysisVo);
        List<String> proCodes = basicAndCz.stream().map(SjxyfxBasic::getProCode).filter(StrUtil::isNotBlank).collect(Collectors.toList());
        if (CollUtil.size(proCodes) > 500) {
            proCodes = new ArrayList<>();
        }
        List<SjxyfxBasic> klCb = sjxyfxMapper.getKlCb(proCodes);
        List<SjxyfxBasic> bnCb = sjxyfxMapper.getBnCb(proCodes);
        Map<String, BigDecimal> klcbMap = klCb.stream().collect(Collectors.toMap(SjxyfxBasic::getProCode,
                SjxyfxBasic::getLjcb,(v1,v2) -> v2));
        Map<String, BigDecimal> bncbMap = bnCb.stream().collect(Collectors.toMap(SjxyfxBasic::getProCode,
                SjxyfxBasic::getBncb,(v1,v2) -> v2));
        if (CollUtil.isNotEmpty(basicAndCz)) {
            for (SjxyfxBasic sjxyfxBasic : basicAndCz) {
                String proCode = sjxyfxBasic.getProCode();
                BigDecimal klcb = klcbMap.getOrDefault(proCode, BigDecimal.ZERO);
                BigDecimal bncb = bncbMap.getOrDefault(proCode, BigDecimal.ZERO);
                sjxyfxBasic.setLjcb(klcb);
                sjxyfxBasic.setBncb(bncb);
            }
        }
        return basicAndCz;
    }

}
